﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Resources;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Drawing;

namespace FocusClock.WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            InitNotifyIcon();

            this.panel_running.Margin = new Thickness { Top = 0, Right = 0, Left = 0 };
            this.Width = 182;
            this.Height = 27;
            //this.panel_running.Visibility = Visibility.Hidden;

            InitLocation();

            var context = new ViewModel();
            this.DataContext = context;
        }

        private void InitLocation()
        {
            this.Topmost = true;
            int taskbarHeight = Screen.PrimaryScreen.WorkingArea.Bottom - Screen.PrimaryScreen.WorkingArea.Top;
            this.Left = SystemParameters.PrimaryScreenWidth - this.Width;
            this.Top = taskbarHeight-this.Height;
        }

        private void panel_stop_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
                e.Handled = true;
            }
        }

        private void panel_running_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
                e.Handled = true;
            }
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var textBox = sender as System.Windows.Controls.TextBox;
            var text = textBox.Text;
            var regext = new Regex(@"^[1-9]\d*$");

            if ((!string.IsNullOrWhiteSpace(text)) && !regext.IsMatch(text))
            {
                textBox.Text = "";
            }
        }

        private void InitNotifyIcon()
        {
            var contextMenuStrip1 = new ContextMenuStrip();
            var ExitToolStripMenuItem = new ToolStripMenuItem();

            var notifyIcon1 = new NotifyIcon();
            notifyIcon1.ContextMenuStrip = contextMenuStrip1;
            notifyIcon1.Icon = new Icon(System.IO.Path.Combine(Environment.CurrentDirectory,"src","icon.ico"));
            notifyIcon1.Text = "专注时钟";
            notifyIcon1.Visible = true;
            notifyIcon1.DoubleClick += notifyIcon1_DoubleClick;
            // 
            // contextMenuStrip1
            // 
            
            contextMenuStrip1.Items.AddRange(new ToolStripItem[] { ExitToolStripMenuItem });
            contextMenuStrip1.Name = "contextMenuStrip1";
            contextMenuStrip1.Size = new System.Drawing.Size(101, 26);
            // 
            // 退出ToolStripMenuItem
            // 
            
            ExitToolStripMenuItem.Name = "ExitToolStripMenuItem";
            ExitToolStripMenuItem.Size = new System.Drawing.Size(100, 22);
            ExitToolStripMenuItem.Text = "退出";
            ExitToolStripMenuItem.Click += ExitToolStripMenuItem_Click;
        }

        private void ExitToolStripMenuItem_Click(object? sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void notifyIcon1_DoubleClick(object? sender, EventArgs e)
        {
            InitLocation();
        }
    }
}
